package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjGoodsParam;
import com.zb.entity.ZjSpareRelationParam;
import com.zb.mapper.ZjSpareRelationParamMapper;
import com.zb.service.ZjGoodsParamService;
import com.zb.service.ZjSpareParamFieldValService;
import com.zb.service.ZjSpareRelationParamService;
import com.zb.vo.GoodsCustomParamVo;
import com.zb.vo.GoodsDetailParamVo;
import com.zb.vo.GoodsFieldValVo;
import com.zb.vo.GoodsParamValVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 备件参数关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-15
 */
@Service
public class ZjSpareRelationParamServiceImpl extends ServiceImpl<ZjSpareRelationParamMapper, ZjSpareRelationParam> implements ZjSpareRelationParamService {
    @Resource
    private ZjSpareParamFieldValService fieldValService;
    @Resource
    private ZjGoodsParamService goodsParamService;

    // 新增、修改备件参数
    @Override
    public void save(Long custAccId, Long spareId, GoodsParamValVo paramValVo) {
        if (null == paramValVo.getParamId() || paramValVo.getParamId() == 0) {
            return;
        }
        ZjSpareRelationParam param = new ZjSpareRelationParam();
        BeanUtils.copyProperties(paramValVo, param);
        if (null != param.getId()) {
            this.updateById(param);
        } else {
            param.setId(IdWorker.getId());
            param.setCustAccId(custAccId);
            param.setSpareId(spareId);
            this.save(param);
        }
        fieldValService.save(custAccId, param.getId(), paramValVo.getFieldValVos());
    }

    // 根据备件ID获取备件参数、参数字段
    @Override
    public GoodsDetailParamVo getBySpareId(Long spareId) {
        QueryWrapper<ZjSpareRelationParam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "param_id");
        wrapper.eq("spare_id", spareId);
        ZjSpareRelationParam param = this.getOne(wrapper);
        if (null == param) {
            return null;
        }
        ZjGoodsParam goodsParam = goodsParamService.getById(param.getParamId());
        List<GoodsCustomParamVo> paramFields = fieldValService.getBySpareRelationParamId(param.getId()).stream().map(vo -> {
            GoodsCustomParamVo field = new GoodsCustomParamVo();
            BeanUtil.copyProperties(vo, field);
            return field;
        }).toList();

        GoodsDetailParamVo paramVo = new GoodsDetailParamVo();
        paramVo.setName(null != goodsParam? goodsParam.getName() : "");
        paramVo.setParamFields(paramFields);
        return paramVo;
    }

    // 根据备件ID删除备件参数
    @Override
    public void deleteBatchBySpareIds(List<Long> spareIds) {
        if (null == spareIds || spareIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjSpareRelationParam> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.in("spare_id", spareIds);
        List<ZjSpareRelationParam> list = this.list(wrapper);
        List<Long> ids = list.stream().map(ZjSpareRelationParam::getId).toList();
        fieldValService.deleteBatchBySpareRelationParamIds(ids);
        this.removeByIds(ids);
    }

    // 根据备件ID获取备件参数
    @Override
    public GoodsParamValVo getParamBySpareId(Long spareId) {
        QueryWrapper<ZjSpareRelationParam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "param_id");
        wrapper.eq("spare_id", spareId);
        ZjSpareRelationParam param = this.getOne(wrapper);
        if (null == param) {
            return null;
        }
        GoodsParamValVo paramValVo = new GoodsParamValVo();
        BeanUtils.copyProperties(param, paramValVo);
        paramValVo.setFieldValVos(fieldValService.getBySpareRelationParamId(param.getId()).stream().map(field -> {
            GoodsFieldValVo fieldValVo = new GoodsFieldValVo();
            BeanUtils.copyProperties(field, fieldValVo);
            return fieldValVo;
        }).toList());
        return paramValVo;
    }
}
